﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CoolCode.Web.UI.WebControls
{
    [DefaultProperty("Mapping")]
    [ToolboxData("<{0}:XCheckBoxField />")]
    public class XCheckBoxField : CheckBoxField 
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("Y/N")]
        [Localizable(true)]
        [Description("布尔值转换定义，格式为:True/False. 当设定为 T/F 时，表示 T 为 True，F 为 False ; 当设定为 T/* 时，表示 T 为 True，其它为 False")]
        public string Mapping
        {
            get
            {
                return  (String)(ViewState["Mapping"]??"Y/N"); 
            }

            set
            {
                ViewState["Mapping"] = value;
            }
        }

        private bool valueToBoolean(object value)
        {
            if(value ==null)return false ;
            if (this.Mapping == null) return false;
            string [] spilt= this.Mapping .Split('/');
            if(spilt .Length !=2)
                throw new NotSupportedException ("Mapping: "+this.Mapping );
            string trueText = spilt[0];
            string falseText = spilt[1];
            string curText = value.ToString();
            return trueText.Equals(curText);
        }

        private string  booleanToValue(bool b)
        { 
            if (this.Mapping == null) return string.Empty ;
            string[] spilt = this.Mapping.Split('/');
            if (spilt.Length != 2)
                throw new NotSupportedException("Mapping: " + this.Mapping);
            string trueText = spilt[0];
            string falseText = spilt[1]; 
            return b? trueText:falseText;
        }

        protected override void OnDataBindField(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Mapping))
            {
                base.OnDataBindField(sender, e);
            }
            else
            {
                if (sender is CheckBox)
                {

                    CheckBox c = (CheckBox)sender;
                    Control cn= c.NamingContainer;
                    object value = this.GetValue(cn);
                    if (value == DBNull.Value)
                    {
                        c.Checked = false;
                    }
                    else if (value is bool)
                    {
                        c.Checked = (bool )value;
                    }
                    else
                    {
                        if (!this.DesignMode)
                        {
                            c.Checked = valueToBoolean(value);
                        }
                    }
                }
            }
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            if (string.IsNullOrEmpty(this.Mapping))
            {
                base.ExtractValuesFromCell(dictionary, cell, rowState, includeReadOnly);
            }
            else
            {
                if (cell.Controls.Count > 0)
                {
                    CheckBox c = (CheckBox)cell.Controls[0];
                    if (c != null && (c.Enabled || includeReadOnly))
                    {                       
                        string  value  = booleanToValue(c.Checked);
                        if(!string.IsNullOrEmpty(value))
                        {
                            if (dictionary.Contains(this.DataField))
                                dictionary[this.DataField] = value;
                            else
                                dictionary.Add(this.DataField, value);
                        }
                    }
                }
            }
        }

        //protected override void RenderContents(HtmlMappingWriter output)
        //{
        //    output.Write(Mapping);
        //}
    }
}
